package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"house/api/resp"
	"house/internal/dao"
	"house/internal/model"
	"log"
	"path/filepath"
)

func AddImg(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.TypeUser == 2 || user.TypeUser == 3) {
		resp.Error(c, 100000, "您没有权限上传图片", false)
		return
	}

	dst := "D:\\GO-Work\\src\\house\\static\\upload\\" + file.Filename
	// 上传文件至指定的完整文件路径
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		resp.Error(c, 100000, "上传图片失败", false)
		return
	}
	resp.Success(c, "上传图片成功", fmt.Sprintf("http://127.0.0.1:8080/upload/"+file.Filename))

}

func AddVideo(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.TypeUser == 2 || user.TypeUser == 3) {
		resp.Error(c, 100000, "您没有权限上传视频", false)
		return
	}

	ext := filepath.Ext(file.Filename)
	if ext != ".mp4" {
		resp.Error(c, 100000, "视频格式必须是.mp4格式，请重新上传", false)
		return
	}
	if file.Size > 200*1024*1024 {
		resp.Error(c, 100000, "视频大小不能超过200MB，请重新上传", false)
		return
	}

	dst := "D:\\GO-Work\\src\\house\\static\\upload\\" + file.Filename
	// 上传文件至指定的完整文件路径
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		resp.Error(c, 100000, "上传图片失败", false)
		return
	}
	resp.Success(c, "上传图片成功", fmt.Sprintf("http://127.0.0.1:8080/upload/"+file.Filename))
}
